package com.ansion.basedb.util;

import android.text.TextUtils;

import com.ansion.basedb.R;
import com.ansion.basedb.dao.helper.UserHelper;
import com.ansion.basedb.dao.model.UserModel;
import com.ansion.utils.ContactUtil;
import com.ansion.utils.ResourceUtils;

/**
 * created by zhaoyuntao
 * on 18/03/2022
 * description:
 */
public class UserModelNameUtils {
    public static String[] sampleStatusArray;

    public static String getDisPlayNote(UserModel userModel) {
        String note = userModel.getNote();
        if (userModel.getStatus_type() == UserModel.STATUS_TYPE_CUSTOM) {
            return note;
        } else if (userModel.getStatus_type() == UserModel.STATUS_TYPE_SYS) {
            try {
                int num = TextUtils.isEmpty(note) ? 0 : Integer.parseInt(note);
                if (sampleStatusArray == null) {
                    sampleStatusArray = ResourceUtils.getContext()
                            .getResources()
                            .getStringArray(R.array.stausSamples);
                }
                return sampleStatusArray[num];
            } catch (Exception ignore) {
            }
        } else {
            return "";
        }
        return note;
    }

    /***
     *是否是通讯录好友
     * @param userModel
     * @return
     */
    public static boolean isContactUser(UserModel userModel) {
        return !userModel.isOnlyLocal() || userModel.isBabaTeam() || userModel.isSomaNews();
    }

    // 群聊中收到的Notification，若对方存在通讯录里则显示通讯录保存的姓名；
    // 若对方是陌生人（没有将对方姓名保存到通讯录）则显示对方自己在Profile里面设置的名称；
    // 若对方是陌生人并没有设置Profile姓名则显示电话号码
    public static String getNotificationName(UserModel userModel, boolean phoneFormat) {
        return getDisplayName(userModel, phoneFormat);
    }

    public static String getDisplayName(UserModel userModel) {
        return getDisplayName(userModel, true);
    }

    public static String getDisplayName(UserModel userModel, boolean phoneFormat) {
        return getDisplayNameForNameView(userModel, false, phoneFormat);
    }


    public static String getDisplayNameForNameView(UserModel userModel, boolean showNickname) {
        return getDisplayNameForNameView(userModel, showNickname, true);
    }

    public static String getDisplayNameForNameView(UserModel userModel, boolean showNickname, boolean phoneFormat) {
        if (userModel == null) {
            return null;
        }
        long userId = userModel.getUserId();
//        if (ContactUtil.isFileAssistant(userId)) {
//            return ResourceUtils.getString(R.string.baba_feedback_file_assistant);
//        }
//        if (ContactUtil.isNewBotimTeam(String.valueOf(userId)) && TextUtils.isEmpty(userModel.getNickName())) {
//            return ResourceUtils.getString(R.string.baba_baba);
//        }
//        if (ContactUtil.isPayOfficial(String.valueOf(userId)) && TextUtils.isEmpty(userModel.getNickName())) {
//            return ResourceUtils.getString(R.string.common_botpay);
//        }
//        if (ContactUtil.isSomaNews(userId)) {
//            return ResourceUtils.getString(R.string.baba_somanews);
//        }
        if (ContactUtil.isSystemId(userId)) {
            return userModel.getNickName();
        }
        if (ContactUtil.isOAAccount(userId)) {
            return userModel.getNickName();
        }
        if (null != userModel.getContactModel()) {
            return userModel.getContactModel().getDisplayName(phoneFormat);
        } else {
            if (!TextUtils.isEmpty(userModel.getNickName())) {
                if (showNickname || ContactUtil.isMySelf(userId)) {//自己有昵称则直接显示
                    return userModel.getNickName();
                }
            }
            String result = phoneFormat ? ContactUtil.getFormatPhone(userId) : "+" + userId;
            return ResourceUtils.getString(R.string.bot_uid_ltr, result);
        }
    }

    public static String getDisplayName(long uid, boolean isNickName) {
        String username = null;
        UserModel user = UserHelper.getUserModel(uid);
        if (null != user) {
            if (isNickName) {
                username = user.getNickName();
            } else {
                username = getDisplayNameForNameView(user, false);
            }
        }

        if (TextUtils.isEmpty(username)) {
            return uid + "";
        }

        return username;
    }
}
